// pages/login/msgLogin.js
const app = getApp()
import { sendMsg, msgLogin } from '../../api/user'
import {
  lockSysUser
} from '../../api/member'
import utils from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {

    verifyCodeKey: '',
    lineShow: true,
    phoneNumber: '',
    isShow: true,
    second: 60,
    vCode: '',
    code:"",
    SHARE_TYPE:0,
    xuid:null
  },
  startLockSysUser() {
    lockSysUser({ mchId: app.globalData.mchId, xuid: app.globalData.xuid ,shopId:app.globalData.shopId}).then(res => {
      console.log(res)
      wx.showToast({
        title: res.content,
        icon: 'none',
        duration: 2000
      })
    })
  },
  loginClick() {
    var _this = this;
    console.info("app.globalData.uuid--"+app.globalData.uuid)
    if (this.data.vCode.length == 4) {
      let mchId = app.globalData.mchId;
      if (app.APP_TYPE > 0) {
        mchId = app.ISV_ID
      }

      let username = 'mch_'+mchId+'_'+this.data.phoneNumber;
      msgLogin({
        mchId: mchId,
        username: username,
        wxmId: app.globalData.uuid,
        verifyCode: this.data.vCode,
        code:this.data.code
      }).then(res => {
        if (res.type == 'success') {
          const {
            userInfo,
            token
          } = res.data
          console.info("res.data--" + JSON.stringify(res.data))
          console.info("token--" + JSON.stringify(token))

          app.globalData.token = token
          console.info("app.globalData.token--" + JSON.stringify(app.globalData.token))
          app.globalData.userInfo = userInfo

          _this.startLockSysUser()
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      }).catch(error => {
        wx.showToast({
          title: error.content,
          icon: 'none',
          duration: 2000
        })
      })
    } else {

    }

  },
  inputPhoneNum(e) {
    this.data.phoneNumber = e.detail.value
    this.setData({
      phoneNumber: this.data.phoneNumber
    })
  },
  inputcodeNum(e){
    this.data.code = e.detail.value
    this.setData({
      code: this.data.code
    })
  } ,
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              isShow: true,
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  verificationCode(e) {

    this.data.vCode = e.detail.value

  },
  verificationClick() {
    if (this.data.phoneNumber == "" || this.data.phoneNumber.length != 11 || !(/^((1))\d{10}$/.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.timer()
    let mchId = app.globalData.mchId;
    if (app.APP_TYPE > 0) {
      mchId = app.ISV_ID
    }
    sendMsg({
      username: this.data.phoneNumber,
            tplKey: 'register',
      mchId: mchId,
    }).then(res => {

    })
    this.data.isShow = false
    this.setData({
      isShow: this.data.isShow
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.SHARE_TYPE=app.SHARE_TYPE
    this.data.xuid=app.xuid
    this.setData({
      SHARE_TYPE:this.data.SHARE_TYPE,
      xuid:this.data.xuid
    })
  },

})
